package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjbusiness;
import com.chezhujie.www.service.CzjbusinessService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 *   商家信息表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/czjbusiness")
public class CzjbusinessController extends BaseController<Czjbusiness> {
    @Autowired
    private CzjbusinessService czjbusinessService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Czjbusiness czjbusiness, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjbusiness,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjbusiness);
            Pagination pagination = czjbusinessService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取商家信息表列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", czjbusiness);
        return "czjbusiness/list";
    }
    /**
    * api接口商家信息表列表
    * @param pageNo
    * @param pageSize
    * @param czjbusiness
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, Czjbusiness czjbusiness,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(czjbusiness);
            Pagination pagination = czjbusinessService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取商家信息表列表失败!"+e,"",resp);
        }

    }
    /**
    * api接口商家信息表详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = czjbusinessService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取商家信息表详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "czjbusiness/apiTest";

    }

    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Czjbusiness czjbusiness,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(czjbusiness.getId())){
                czjbusinessService.update(czjbusiness);


            }else{
                czjbusiness.setId(UUIDUtils.getUUID());
                czjbusinessService.insert(czjbusiness);
            }
            model.addAttribute("success", "保存商家信息表成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存商家信息表失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String id,Czjbusiness czjbusiness, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjbusiness,req,model);
        try {
            Czjbusiness entity = czjbusinessService.queryById(czjbusiness.getId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询商家信息表出错!详情："+e.getMessage());
        }
        return "czjbusiness/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Czjbusiness czjbusiness, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(czjbusiness,req,model);
        if(StringUtils.isNotBlank(czjbusiness.getId())){
            czjbusiness = czjbusinessService.queryById(czjbusiness.getId());
            model.addAttribute("entity", czjbusiness);
        }
        return "czjbusiness/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String id,Czjbusiness czjbusiness, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            czjbusinessService.deleteById(czjbusiness.getId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }
    /** 审批 **/
    @RequestMapping("/approve.do")
    public String approve(@RequestParam(required = true)String id,
                          @RequestParam(required = true)Integer approveStatus,
                          HttpServletRequest req, HttpServletResponse resp,
                          ModelMap model){

        czjbusinessService.changeApprove(id,approveStatus);
        return "redirect:detail.do?id="+id;
    }

    private void setMsg(Czjbusiness czjbusiness,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", czjbusiness);
    }
}
